package com.xuxingcan;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

public class Main {
    private static HashMap<String, ArrayList<String>> urlListMap = new HashMap<>();
    private static ArrayList<BaseUrlInfo> baseUrlInfos = new ArrayList<>();
    private static ArrayList<String> mArtistList = new ArrayList<>();
    private static String basePath = "F:\\MV";

    static {
        baseUrlInfos.add(new BaseUrlInfo("Fiestar", "http://www.yinyuetai.com/fanclub/mv/25374/toNew"));
        baseUrlInfos.add(new BaseUrlInfo("T-ara", "http://www.yinyuetai.com/fanclub/mv/1559/toNew"));
        baseUrlInfos.add(new BaseUrlInfo("少女时代", "http://www.yinyuetai.com/fanclub/mv/79/toNew"));
        baseUrlInfos.add(new BaseUrlInfo("fx", "http://www.yinyuetai.com/fanclub/mv/1841/toNew"));
        baseUrlInfos.add(new BaseUrlInfo("missA", "http://www.yinyuetai.com/fanclub/mv/7742/toNew"));
        baseUrlInfos.add(new BaseUrlInfo("4Minute", "http://www.yinyuetai.com/fanclub/mv/1053/toNew"));
        baseUrlInfos.add(new BaseUrlInfo("Girl's Day", "http://www.yinyuetai.com/fanclub/mv/8247/toNew"));
        baseUrlInfos.add(new BaseUrlInfo("A Pink", "http://www.yinyuetai.com/fanclub/mv/14484/toNew"));
        baseUrlInfos.add(new BaseUrlInfo("Nine Muses", "http://www.yinyuetai.com/fanclub/mv/7690/toNew"));
        baseUrlInfos.add(new BaseUrlInfo("Hello Venus", "http://www.yinyuetai.com/fanclub/mv/23998/toNew"));
        baseUrlInfos.add(new BaseUrlInfo("AOA", "http://www.yinyuetai.com/fanclub/mv/25339/toNew"));
        baseUrlInfos.add(new BaseUrlInfo("rainbow", "http://www.yinyuetai.com/fanclub/mv/2989/toNew"));
        baseUrlInfos.add(new BaseUrlInfo("secret", "http://www.yinyuetai.com/fanclub/mv/2526/toNew"));
        baseUrlInfos.add(new BaseUrlInfo("Stellar", "http://www.yinyuetai.com/fanclub/mv/17308/toNew"));
        baseUrlInfos.add(new BaseUrlInfo("EXID", "http://www.yinyuetai.com/fanclub/mv/23043/toNew"));
    }

    public static void main(String[] args) {
//        String baseUrl = "http://www.yinyuetai.com/fanclub/shoot/25374/toNew";
        while (baseUrlInfos.size() != 0) {
            getMVUrl(baseUrlInfos.remove(0));
        }
        String artist;
        DistributionThread dt;
        for (int i = 0; i < mArtistList.size(); i++) {
            artist = mArtistList.get(i);
            File file = new File(new File(basePath), artist);
            if (file.exists()) {
                if (!file.isDirectory()) {
                    file.delete();
                    file.mkdir();
                }
            } else {
                file.mkdir();
            }
            dt = new DistributionThread(urlListMap.remove(artist), file.getAbsolutePath());
            dt.start();
            while (true) {
                try {
                    Thread.sleep(60 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (!dt.isRunning()) {
                    break;
                }
            }
        }
    }

    private static void getMVUrl(BaseUrlInfo mBaseUrlInfo) {
        ArrayList<String> urlList = new ArrayList<>();
        String nextUrl = mBaseUrlInfo.getUrl();
        int page = 0;
        while (true) {
            if (page > 0) {
                nextUrl = mBaseUrlInfo.getUrl() + "/" + page;
            }
            String result = HttpUtil.get(nextUrl, "UTF-8");
            Document mDocument = Jsoup.parse(result);
            Elements mElements = mDocument.getElementsByClass("mv_list");
            if (mElements.size() == 0) {
                break;
            }
            mElements = mElements.get(0).getElementsByTag("a");
            int mvCount = mElements.size() / 3;
            for (int i = 0; i < mvCount; i++) {
                Element mElement = mElements.get(i * 3);
                String s = mElement.attr("href");
                urlList.add(s);
            }
            page++;
        }
        urlListMap.put(mBaseUrlInfo.getArtist(), urlList);
        mArtistList.add(mBaseUrlInfo.getArtist());
    }
}
